﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditGameLocation : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        //public event EventHandler<ObjectEventArgs<LocationInt>> RequestGoLocation;
        //public event EventHandler<ObjectEventArgs<LocationInt>> RequestEditGameLocation;

        public override void SetVisualizedData(DataVisualizer vis, object o) { GameLocation = o as GameLocation; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(GameLocation); } }
        public string VisualizerName { get { return "EditGameLocation"; } }

        public static readonly DependencyProperty GameLocationProperty = DependencyProperty.Register(
            "GameLocation", typeof(GameLocation), typeof(EditGameLocation), new PropertyMetadata(null, new PropertyChangedCallback(GameLocationChanged)));

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditGameLocation, T2> action)
        {
            d.HandleChange<EditGameLocation, T2>(e, action, (t) => t.UpdateBindings());
        }

        public GameLocation GameLocation
        {
            get { return (GameLocation)GetValue(GameLocationProperty); }
            set { SetValue(GameLocationProperty, value); }
        }

        private static void GameLocationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<GameLocation>(d, e, (t, val) => t.GameLocation = val);
        }

        public override void UpdateBindings()
        {
            if (GameLocation != null)
            {
                if (gameLoc != null)
                    gameLoc.DataContext = GameLocation;

                GameLocation loc = gameLoc.DataContext as GameLocation;

                if (loc == null || loc.Things == null || loc.Things.Count == 0)
                {
                    showThings.Visibility = Visibility.Collapsed;
                }
                else
                {
                    showThings.Visibility = Visibility.Visible;
                }
            }
        }

        public override string TitleText
        {
            get
            {
                if (GameLocation == null)
                    return string.Empty;

                return string.Format("Loc {0}, {1}", GameLocation.XYZ.ToString(),
                    GameLocation.ShortName == null ? "No Name" : GameLocation.ShortName);
            }
        }


        public EditGameLocation()
        {
            InitializeComponent();
        }

        private void ShowProps_Loaded(object sender, RoutedEventArgs e)
        {
            //editProperties.wc.Indent = Window.Indent + 1;
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreGameLocationAsync(DataAccess.AssignedUniqueId, GameLocation);
        }

        private void showGameThings_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void EditAssociatedIDCollection_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }

        private void ShowEdges_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void DataVisualizer_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }

        private void DataVisualizer_RequestEditID_1(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }

        private void DataVisualizer_RequestEditID_2(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }

        //public void OnRequestEditGameLocation(object sender, ObjectEventArgs<LocationInt> e)
        //{
        //    if (RequestEditGameLocation != null)
        //        RequestEditGameLocation(this, e);
        //}

        private void EditLocationInt_RequestGoLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            OnRequestGoToLocation(sender, e);
            //if (RequestGoLocation != null)
            //    RequestGoLocation(this, e);
        }

        private void EditLocationInt_RequestEditGameLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            OnRequestEditGameLocation(sender, e);
        }
    }
}